/*
 * Copyright 2020-2030 ALPHA LAB.Inc All Rights Reserved.
 */
package com.alphalab.framework.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * spring工具类.
 * @author allen
 * @version 1.0.0 2020年3月15日
 */
@Component
public final class SpringUtil implements ApplicationContextAware {
	
    private static ApplicationContext applicationContext;
    
	private SpringUtil() {
	}
    
	/**
	 * 设置ApplicationContext.
	 * @param context ApplicationContext
	 */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) 
    		throws BeansException {
        if (SpringUtil.applicationContext == null) {
            SpringUtil.applicationContext = applicationContext;
        }
    }
 
    /**
     * 获取applicationContext.
     * @return ApplicationContext
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }
 
	/**
	 * 获取spring的bean对象.
	 * @param beanId bean对象名
	 * @return Object
	 */
    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }
 
	/**
	 * 获取spring的bean对象.
	 * @param <T> class名
	 * @return <T>
	 */
    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }
 
}
